package com.hskn.hss.module.caseset.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hskn.hss.exception.HssException;
import com.hskn.hss.module.caseset.service.ICasesetService;
import com.hskn.hss.module.casesetissue.entity.CasesetIssue;
import com.hskn.hss.module.casesetissue.service.ICasesetIssueService;
import com.hskn.hss.module.casesetissuesource.entity.CasesetIssueSource;
import com.hskn.hss.module.casesetissuesource.service.ICasesetIssueSourceService;
import com.hskn.hss.module.casesetlevel.entity.CasesetLevel;
import com.hskn.hss.module.casesetlevel.service.ICasesetLevelService;
import com.hskn.hss.module.casesetsys.entity.CasesetSys;
import com.hskn.hss.module.casesetsys.mapper.CasesetSysMapper;
import com.hskn.hss.module.casesetsys.service.ICasesetSysService;
import com.hskn.hss.utils.StringUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author hskn
 * @since 2021-05-18
 */
@Service
public class CasesetServiceImpl implements ICasesetService {

    @Autowired
    ICasesetLevelService casesetLevelService;

    @Autowired
    ICasesetIssueService casesetIssueService;

    @Autowired
    ICasesetIssueSourceService casesetIssueSourceService;

    @Autowired
    ICasesetSysService casesetSysService;

    @Resource
    CasesetSysMapper casesetSysMapper;

    @Override
    @Transactional
    public void mUpdateBatch(JSONObject params) throws HssException {

        if (params.containsKey("issue")) {
            JSONArray issueArr = params.getJSONArray("issue");
            if (StringUtils.isNotEmpty(issueArr)) {
                List<CasesetIssue> casesetIssueList = new ArrayList<>();
                for (Object issue : issueArr) {
                    casesetIssueList.add(JSON.parseObject(issue.toString(), CasesetIssue.class));
                }
                casesetIssueService.saveOrUpdateBatch(casesetIssueList);
            }
        }
        if (params.containsKey("issuesource")) {
            JSONArray issuesourceArr = params.getJSONArray("issuesource");
            if (StringUtils.isNotEmpty(issuesourceArr)) {
                List<CasesetIssueSource> casesetIssueSourceList = new ArrayList<>();
                for (Object issuesource : issuesourceArr) {
                    casesetIssueSourceList.add(JSON.parseObject(issuesource.toString(), CasesetIssueSource.class));
                }
                casesetIssueSourceService.saveOrUpdateBatch(casesetIssueSourceList);
            }
        }
        if (params.containsKey("level")) {
            JSONArray levelArr = params.getJSONArray("level");
            if (StringUtils.isNotEmpty(levelArr)) {
                List<CasesetLevel> casesetLevelList = new ArrayList<>();
                for (Object level : levelArr) {
                    casesetLevelList.add(JSON.parseObject(level.toString(), CasesetLevel.class));
                }
                casesetLevelService.saveOrUpdateBatch(casesetLevelList);
            }
        }
        if (params.containsKey("sys")) {
            JSONObject sys = params.getJSONObject("sys");
            CasesetSys casesetSys = JSON.parseObject(sys.toJSONString(), CasesetSys.class);
            casesetSys.setCasesetSysId("1");
            casesetSysMapper.updateById(casesetSys);
        }
    }

    @Override
    public Boolean saveOrUpdateBatchCasesetIssue(JSONArray params) {
        if (params != null) {
            List<CasesetIssue> casesetIssueList = JSON.parseArray(params.toString(), CasesetIssue.class);
            if (CollectionUtils.isNotEmpty(casesetIssueList)) {
                return casesetIssueService.saveOrUpdateBatch(casesetIssueList);
            }
        }
        return false;
    }

    @Override
    public Boolean saveOrUpdateBatchCasesetIssueSource(JSONArray params) {
        if (params != null) {
            List<CasesetIssueSource> casesetIssueSourceList = JSON.parseArray(params.toString(), CasesetIssueSource.class);
            if (CollectionUtils.isNotEmpty(casesetIssueSourceList)) {
                return casesetIssueSourceService.saveOrUpdateBatch(casesetIssueSourceList);
            }
        }
        return false;
    }

    @Override
    public Boolean saveOrUpdateBatchCasesetLevel(JSONArray params) {
        if (params != null) {
            List<CasesetLevel> casesetLevelList = JSON.parseArray(params.toString(), CasesetLevel.class);
            if (CollectionUtils.isNotEmpty(casesetLevelList)) {
                return casesetLevelService.saveOrUpdateBatch(casesetLevelList);
            }
        }
        return false;
    }

    @Override
    public Integer casesetSysUpdateById(JSONObject sys) {
        CasesetSys casesetSys = JSON.parseObject(sys.toJSONString(), CasesetSys.class);
        casesetSys.setCasesetSysId("1");
        return casesetSysMapper.updateById(casesetSys);
    }

}
